#要将字符串写入文件中，要先把字符串编码为字节串, 因为计算机底层存储的是字节串，字符串只不过是通过解码展示给我们看的
#可以理解为，字节串是给机器读的，字符串是给人读的，这两个之间的媒介就是编码方式
#文本模式 open()函数  file mode=r encoding  ,这里会按照encoding的值返回文件对象
# write() close() read() readlines() 
# 二进制模式 mode=rb 这个时候encoding就不用写了
#任何文件都是以字节的方式存储的，那么对文件的传输，拷贝实际上都是对字节串进行处理
#with ---- as 
# flush()
# python 文件本质也是 字节串 ，python解释器读取的时候要编码为   字符串，才能解释执行，所以有些时候需要申明文件的编码方式 方式：在文件的第一行或者第二行加上 # coding=xxx，linux不一样哈

# lujing1 = '/Users/show/Downloads/编程/wenben.txt'
# lujing2 = '/Users/show/Desktop/截屏2023-03-27 02.57.01.png'

# f = open(lujing2,'rb')
# content = f.read()
# f.close()
# # print(content)

# f = open(lujing1,'ab')
# content = b'\xe7\x99\xbd\xe6\x9c\x88\xe9\xbb\x91\xe7\xbe\xbd'
# f.write(content)
# f.close()


# #对文件的拷贝功能
# def copy_somethin(src_path,tar_path):
#     f = open(src_path,'rb')
#     content = f.read()
#     f.close()


#     f = open(tar_path,'wb')
#     f.write(content)
#     f.close

# copy_somethin('/Users/show/Downloads/编程/wenben.txt','/Users/show/Downloads/编程/wenben1fuzhi.txt')

####    对图片一样可以
# copy_somethin('/Users/show/Desktop/截屏2023-03-27 02.57.01.png','/Users/show/Downloads/编程/tupian.png')


# file_path = '/Users/show/Downloads/编程/python学习/0013_a1.txt'
# with open(file_path,'r',encoding='UTF8') as f:
#     content = f.read()
#     file_info = content.splitlines()
#     str1 = ''
#     for i in file_info:
#         if i and i.count(':') == 1 :
#             name,age=i.split(':')
#             if int(age.strip()) > 50:
#                 str1 += f'{name.strip()},'
    
#     str2 = str1[:-1]
# with open(file_path,'a',encoding='UTF8') as f:
#     f.write(f'\n大于50岁的人有：{str2}')


#额外练习
# path1 = '/Users/show/Downloads/编程/python学习/cfiles/gbk编码.txt'
# path2 = '/Users/show/Downloads/编程/python学习/cfiles/utf8编码.txt'

# with open(path1,'r',encoding='GBK') as f :
#     content1 =f.read()

# with open(path2,'r',encoding='UTF8') as f :
#     content2 =f.read()

# print(f'合并以后的内容是：{content1}\n{content2}')

# file_name = input('请输入文件名：')
# new_path = '/Users/show/Downloads/编程/python学习/cfiles/'+file_name+'.txt'
# with open(new_path,'w',encoding='utf8') as f:
#     f.write(f'{content1}\n{content2}')

path3 = '/Users/show/Downloads/编程/python学习/prac_filerw.txt'

with open(path3,'r',encoding='utf8') as f:
    content =  f.read()
    taget = 'https://www.bilibili.com/video/av74106411/?p='
    index1 = -1
    while True:
        index1 = content.find(taget,index1+1)
        if index1 == -1:
            break
        length1 = len(taget)

        num = ''
        num_length = 0
        digit_index = index1+length1
        while True:
            if content[digit_index].isdigit():
                num += content[index1+length1]
                num_length += 1
                digit_index += 1
            else:
                break
        new_num = int(num)+3

        pre = content[:index1+length1]
        last = content[index1+length1+num_length:]
        new = f'{pre}{new_num}{last}'
        content = new
        
# print(content)

import re
with open(path3,'r',encoding='utf8') as f:
    comtent2 = f.read()

cmd = r'p=(\d+)'
def tihuan(match):
    src = match[0]
    p = match[1]
    new = int(p)+3
    print(f'匹配上了{src},把{p}替换为{new}')
    return f'p={new}'
new_str = re.sub(cmd,tihuan,comtent2)    
print(new_str)



# new_file = f'/Users/show/Downloads/编程/python学习/'+'new.txt'
# with open(new_file,'w',encoding='utf8') as f:
#     f.write(content)




